package com.wang.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.exception.BusinessException;
import com.wang.param.PostCollectionQueryParam;
import com.wang.param.ShopQueryParam;
import com.wang.reponse.DishesVo;
import com.wang.reponse.EvaluationVo;
import com.wang.reponse.PostVo;
import com.wang.reponse.ShopVo;
import com.wang.user.model.Dishes;
import com.wang.user.model.Shop;
import com.wang.user.model.ShopCollection;
import com.wang.user.model.User;
import com.wang.user.service.*;
import com.wang.user.mapper.ShopMapper;
import com.wang.user.service.user.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author bkty0
* @description 针对表【shop(商铺)】的数据库操作Service实现
* @createDate 2025-04-23 10:39:21
*/
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop>
    implements ShopService{

    @Resource
    private ShopMapper shopMapper;

    @Resource
    private DishesService dishesService;

    @Resource
    private UserService userService;

    @Resource
    private ShopCollectionService shopCollectionService;

    @Resource
    private PostService postService;

    @Resource
    private EvaluationService evaluationService;

    @Override
    public List<Shop> listShop(ShopQueryParam queryParam) {
        List<Shop> shops = shopMapper.listByParam(queryParam);
        if(CollUtil.isEmpty(shops)){
            return Collections.emptyList();
        }
        shops = shops.stream().peek(item -> {
            List<EvaluationVo> evaluationVoList = evaluationService.getByShopId(item.getId());
            if(CollUtil.isNotEmpty(evaluationVoList)){
                int totalCount = 0;
                for (EvaluationVo evaluationVo : evaluationVoList) {
                    if(ObjectUtil.isNull(evaluationVo.getLevel())){
                        evaluationVo.setLevel(0);
                    }
                    totalCount += evaluationVo.getLevel();
                }
                BigDecimal averageBigDecimal = BigDecimal.valueOf(totalCount)
                        .divide(BigDecimal.valueOf(evaluationVoList.size()), 1, RoundingMode.HALF_UP);
                item.setScore(averageBigDecimal);
            }else {
                item.setScore(BigDecimal.ZERO);
            }
        }).collect(Collectors.toList());
        return shops;
    }

    @Override
    public ShopVo getShopById(Long id) {
        Shop shop = getById(id);
        if(shop == null){
            throw new BusinessException("商铺信息不存在");
        }

        User loginUser = userService.getLoginUser();
        ShopCollection byUserIdAndShopId = shopCollectionService.getByUserIdAndShopId(loginUser.getUid(), shop.getId());

        ShopVo shopVo = new ShopVo();
        BeanUtils.copyProperties(shop,shopVo);
        shopVo.setIsCollect(ObjectUtil.isNotNull(byUserIdAndShopId));

        //当前商铺的相关帖子
        PostCollectionQueryParam postCollectionQueryParam = new PostCollectionQueryParam();
        postCollectionQueryParam.setShopId(shopVo.getId());
        shopVo.setPostVoList(postService.listByShopId(postCollectionQueryParam));

        //当前商铺的评价
        List<EvaluationVo> evaluationVoList = evaluationService.getByShopId(shopVo.getId());
        shopVo.setEvaluationVoList(evaluationVoList);
        if(CollUtil.isNotEmpty(evaluationVoList)){
            int totalCount = 0;
            for (EvaluationVo evaluationVo : evaluationVoList) {
                if(ObjectUtil.isNull(evaluationVo.getLevel())){
                    evaluationVo.setLevel(0);
                }
                totalCount += evaluationVo.getLevel();
            }
            BigDecimal averageBigDecimal = BigDecimal.valueOf(totalCount)
                    .divide(BigDecimal.valueOf(evaluationVoList.size()), 1, RoundingMode.HALF_UP);
            shopVo.setScore(averageBigDecimal);
        }else {
            shopVo.setScore(BigDecimal.ZERO);
        }

        return shopVo;
    }

    @Override
    public Boolean deleteShop(Long id) {
        Shop shop = getById(id);
        if(shop==null){
            throw new BusinessException("数据不存在");
        }
        return removeById(id);
    }

    @Override
    public Boolean updateShop(Shop shop) {
        Shop oldShop = getById(shop.getId());
        if(oldShop==null){
            throw new BusinessException("数据不存在");
        }
        return updateById(shop);
    }

    @Override
    public Boolean add(Shop shop) {
        return save(shop);
    }

    @Override
    public ShopVo getShopById2(Long shopId) {
        Shop shop = getById(shopId);
        if(shop == null){
            throw new BusinessException("商铺信息不存在");
        }

        User loginUser = userService.getLoginUser();
        ShopCollection byUserIdAndShopId = shopCollectionService.getByUserIdAndShopId(loginUser.getUid(), shop.getId());

        ShopVo shopVo = new ShopVo();
        BeanUtils.copyProperties(shop,shopVo);
        shopVo.setIsCollect(ObjectUtil.isNotNull(byUserIdAndShopId));

        //当前商铺的相关帖子
        List<PostVo> postVos = postService.listByShopId2(shopVo.getId());
        shopVo.setPostVoList(postVos);

        //当前商铺的评价
        List<EvaluationVo> evaluationVoList = evaluationService.getByShopId(shopVo.getId());
        shopVo.setEvaluationVoList(evaluationVoList);
        if(CollUtil.isNotEmpty(evaluationVoList)){
            int totalCount = 0;
            for (EvaluationVo evaluationVo : evaluationVoList) {
                if(ObjectUtil.isNull(evaluationVo.getLevel())){
                    evaluationVo.setLevel(0);
                }
                totalCount += evaluationVo.getLevel();
            }
            BigDecimal averageBigDecimal = BigDecimal.valueOf(totalCount)
                    .divide(BigDecimal.valueOf(evaluationVoList.size()), 1, RoundingMode.HALF_UP);
            shopVo.setScore(averageBigDecimal);
        }else {
            shopVo.setScore(BigDecimal.ZERO);
        }

        return shopVo;
    }

    @Override
    public ShopVo getShopById3(Long shopId) {
        Shop shop = getById(shopId);
        if(shop == null){
            throw new BusinessException("商铺信息不存在");
        }

        User loginUser = userService.getLoginUser();
        ShopCollection byUserIdAndShopId = shopCollectionService.getByUserIdAndShopId(loginUser.getUid(), shop.getId());

        ShopVo shopVo = new ShopVo();
        BeanUtils.copyProperties(shop,shopVo);
        shopVo.setIsCollect(ObjectUtil.isNotNull(byUserIdAndShopId));
        //当前商铺的评价
        List<EvaluationVo> evaluationVoList = evaluationService.getByShopId(shopVo.getId());
        shopVo.setEvaluationVoList(evaluationVoList);
        if(CollUtil.isNotEmpty(evaluationVoList)){
            int totalCount = 0;
            for (EvaluationVo evaluationVo : evaluationVoList) {
                if(ObjectUtil.isNull(evaluationVo.getLevel())){
                    evaluationVo.setLevel(0);
                }
                totalCount += evaluationVo.getLevel();
            }
            BigDecimal averageBigDecimal = BigDecimal.valueOf(totalCount)
                    .divide(BigDecimal.valueOf(evaluationVoList.size()), 1, RoundingMode.HALF_UP);
            shopVo.setScore(averageBigDecimal);
        }else {
            shopVo.setScore(BigDecimal.ZERO);
        }

        return shopVo;
    }

    @Override
    public List<ShopVo> listVoByShopIds(List<Long> shopIds) {
        return shopIds.stream().map(shopId -> {
            User loginUser = userService.getLoginUser();
            ShopCollection byUserIdAndShopId = shopCollectionService.getByUserIdAndShopId(loginUser.getUid(),shopId);

            ShopVo shopVo = new ShopVo();
            Shop shop = getById(shopId);
            BeanUtils.copyProperties(shop,shopVo);
            shopVo.setIsCollect(ObjectUtil.isNotNull(byUserIdAndShopId));

            //当前商铺的相关帖子
            List<PostVo> postVos = postService.listByShopId2(shopVo.getId());
            shopVo.setPostVoList(postVos);

            //当前商铺的评价
            List<EvaluationVo> evaluationVoList = evaluationService.getByShopId(shopVo.getId());
            shopVo.setEvaluationVoList(evaluationVoList);
            if(CollUtil.isNotEmpty(evaluationVoList)){
                int totalCount = 0;
                for (EvaluationVo evaluationVo : evaluationVoList) {
                    if(ObjectUtil.isNull(evaluationVo.getLevel())){
                        evaluationVo.setLevel(0);
                    }
                    totalCount += evaluationVo.getLevel();
                }
                BigDecimal averageBigDecimal = BigDecimal.valueOf(totalCount)
                        .divide(BigDecimal.valueOf(evaluationVoList.size()), 1, RoundingMode.HALF_UP);
                shopVo.setScore(averageBigDecimal);
            }else {
                shopVo.setScore(BigDecimal.ZERO);
            }

            return shopVo;
        }).collect(Collectors.toList());
    }



    @Override
    public List<ShopVo> listVoByShopIds2(List<Long> shopIds, Integer uid) {
        return shopIds.stream().map(shopId -> {
            ShopCollection byUserIdAndShopId = shopCollectionService.getByUserIdAndShopId(uid,shopId);

            ShopVo shopVo = new ShopVo();
            Shop shop = getById(shopId);
            BeanUtils.copyProperties(shop,shopVo);
            shopVo.setIsCollect(ObjectUtil.isNotNull(byUserIdAndShopId));

            //当前商铺的相关帖子
            List<PostVo> postVos = postService.listByShopId2(shopVo.getId());
            shopVo.setPostVoList(postVos);

            //当前商铺的评价
            List<EvaluationVo> evaluationVoList = evaluationService.getByShopId(shopVo.getId());
            shopVo.setEvaluationVoList(evaluationVoList);
            if(CollUtil.isNotEmpty(evaluationVoList)){
                int totalCount = 0;
                for (EvaluationVo evaluationVo : evaluationVoList) {
                    if(ObjectUtil.isNull(evaluationVo.getLevel())){
                        evaluationVo.setLevel(0);
                    }
                    totalCount += evaluationVo.getLevel();
                }
                BigDecimal averageBigDecimal = BigDecimal.valueOf(totalCount)
                        .divide(BigDecimal.valueOf(evaluationVoList.size()), 1, RoundingMode.HALF_UP);
                shopVo.setScore(averageBigDecimal);
            }else {
                shopVo.setScore(BigDecimal.ZERO);
            }

            return shopVo;
        }).collect(Collectors.toList());
    }

    @Override
    public ShopVo getShopByIdAndUid(Long shopId) {
        Shop shop = getById(shopId);
        if(shop == null){
            throw new BusinessException("商铺信息不存在");
        }

        User loginUser = userService.getLoginUser();
        ShopCollection byUserIdAndShopId = shopCollectionService.getByUserIdAndShopId(loginUser.getUid(), shop.getId());

        ShopVo shopVo = new ShopVo();
        BeanUtils.copyProperties(shop,shopVo);
        shopVo.setIsCollect(ObjectUtil.isNotNull(byUserIdAndShopId));

        //当前商铺的相关帖子
        List<PostVo> postVos = postService.listByShopId2(shopVo.getId());
        shopVo.setPostVoList(postVos);

        //当前商铺的评价
        List<EvaluationVo> evaluationVoList = evaluationService.getByShopId(shopVo.getId());
        shopVo.setEvaluationVoList(evaluationVoList);
        if(CollUtil.isNotEmpty(evaluationVoList)){
            int totalCount = 0;
            for (EvaluationVo evaluationVo : evaluationVoList) {
                if(ObjectUtil.isNull(evaluationVo.getLevel())){
                    evaluationVo.setLevel(0);
                }
                totalCount += evaluationVo.getLevel();
            }
            BigDecimal averageBigDecimal = BigDecimal.valueOf(totalCount)
                    .divide(BigDecimal.valueOf(evaluationVoList.size()), 1, RoundingMode.HALF_UP);
            shopVo.setScore(averageBigDecimal);
        }else {
            shopVo.setScore(BigDecimal.ZERO);
        }

        return shopVo;
    }
}




